// 预设的示例脚本
export const sampleScripts = [
  {
    id: "sample-1",
    name: "百度搜索测试",
    description: "打开百度，搜索关键词并截图结果",
    params: {
      "keyword": "要搜索的关键词"
    },
    steps: [
      {
        action: "navigate_to_url",
        args: {
          url: "https://www.baidu.com"
        },
        delay: 1000
      },
      {
        action: "input",
        args: {
          selector: "#kw",
          text: "${keyword}"
        },
        delay: 500
      },
      {
        action: "click",
        args: {
          selector: "#su"
        },
        delay: 1000
      },
      {
        action: "wait",
        args: {
          milliseconds: 2000
        },
        delay: 0
      },
      {
        action: "screenshot",
        args: {},
        delay: 0
      },
      {
        action: "extract",
        args: {
          selector: ".result h3"
        },
        delay: 0
      }
    ]
  },
  {
    id: "sample-2",
    name: "电商商品信息提取",
    description: "打开指定的商品页面，提取价格和评论数",
    params: {
      "url": "商品页面URL"
    },
    steps: [
      {
        action: "navigate_to_url",
        args: {
          url: "${url}"
        },
        delay: 1000
      },
      {
        action: "wait",
        args: {
          milliseconds: 3000
        },
        delay: 0
      },
      {
        action: "execute_js",
        args: {
          code: `
            // 尝试提取商品信息
            const productName = document.querySelector('h1')?.innerText || '未找到商品名';
            const price = document.querySelector('.price, .product-price, [data-price]')?.innerText || '未找到价格';
            const reviewCount = document.querySelector('.review-count, .comments-count')?.innerText || '未找到评论数';
            
            // 返回提取的信息
            return {
              productName,
              price,
              reviewCount,
              url: window.location.href
            };
          `
        },
        delay: 0
      },
      {
        action: "screenshot",
        args: {},
        delay: 0
      }
    ]
  },
  {
    id: "sample-3",
    name: "表单自动填写",
    description: "自动填写表单并提交",
    params: {
      "username": "用户名",
      "email": "电子邮箱"
    },
    steps: [
      {
        action: "navigate_to_url",
        args: {
          url: "https://example.com/form"
        },
        delay: 1000
      },
      {
        action: "input",
        args: {
          selector: "#username",
          text: "${username}"
        },
        delay: 500
      },
      {
        action: "input",
        args: {
          selector: "#email",
          text: "${email}"
        },
        delay: 500
      },
      {
        action: "click",
        args: {
          selector: "#agree-terms"
        },
        delay: 500
      },
      {
        action: "click",
        args: {
          selector: "button[type='submit'], input[type='submit'], .submit-btn"
        },
        delay: 1000
      },
      {
        action: "wait",
        args: {
          milliseconds: 2000
        },
        delay: 0
      },
      {
        action: "screenshot",
        args: {},
        delay: 0
      }
    ]
  }
];

// 初始化时加载示例脚本
export function loadSampleScripts() {
  chrome.storage.local.get('sampleScriptsLoaded', function(data) {
    // 检查示例脚本是否已经加载过
    if (!data.sampleScriptsLoaded) {
      chrome.storage.local.get('testScripts', function(result) {
        let scripts = result.testScripts || [];
        
        // 添加示例脚本
        sampleScripts.forEach(sample => {
          if (!scripts.some(script => script.id === sample.id)) {
            scripts.push(sample);
          }
        });
        
        // 保存脚本和标记
        chrome.storage.local.set({ 
          testScripts: scripts,
          sampleScriptsLoaded: true
        }, function() {
          console.log('示例脚本已加载');
        });
      });
    }
  });
} 